package com.swsc.xapp.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class RemoteService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new RemoteBinder();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        bindService(new Intent(this,RemoteService.class),connection, Context.BIND_AUTO_CREATE);
        return super.onStartCommand(intent, flags, startId);
    }
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //绑定成功
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            //当RemoteService所处进程被干掉就重新启动
            startService(new Intent(RemoteService.this,LocalService.class));
            bindService(new Intent(RemoteService.this,LocalService.class),connection,Context.BIND_IMPORTANT);
        }
    };
    private class RemoteBinder extends Binder {
    }
}
